package xzy.Controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xzy.dao.TeacherMapper;
import xzy.pojo.Teacher;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "teacherUpdate_Servlet")
public class teacherUpdate_Servlet extends HttpServlet {
    ApplicationContext context=new ClassPathXmlApplicationContext("application.xml");
    TeacherMapper teacherMapper= (TeacherMapper) context.getBean("teacherMapper");
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入教师账号修改页面");
        request.setCharacterEncoding("utf-8");
        HttpSession session=request.getSession();
        String teacherName= request.getParameter("userName");
        String teacherPwd=request.getParameter("password");
        try{
            if(teacherPwd.length()>0){
                Teacher teacher=new Teacher(teacherName,teacherPwd);
                int res=teacherMapper.updateTeacherPwd(teacher);
                if(res>0){
                    request.setAttribute("Update_error","修改成功");
                }else {
                    request.setAttribute("Update_error","修改失败");
                }
            }else {
                request.setAttribute("Update_error","密码不可以为空");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            session.setAttribute("teacherName",teacherName);
            request.getRequestDispatcher("/teacherUpdate.jsp?teacherName="+teacherName).
                    forward(request,response);
        }

    }
}
